package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;

/**
 * Created by Sunzhigang on 2016/6/6.
 */
public class DialPhoneDialog extends Dialog {
    private DialPhoneNumberCallBack callBack;
    private String number1;
    private String number2;
    private String countryCode1;
    private String countryCode2;

    private ImageView number_1_iv;
    private ImageView number_2_iv;
    private TextView number_1_tv;
    private TextView number_2_tv;
    private Button dial_btn;
    private Button monitor_btn;
    private View number_2_rl;
    private View number_1_rl;


    private String dial_phone_number = "";
    private String dial_phone_number_code = "";
    private String watchName;
    private Context context;
    private boolean isSingleNumber = false;


    public DialPhoneDialog(Context context, String countryCode1, String countryCode2, String number1, String number2, DialPhoneNumberCallBack callBack) {
        super(context);
        this.callBack = callBack;
        this.number1 = number1;
        this.number2 = number2;
        this.countryCode1 = countryCode1;
        this.countryCode2 = countryCode2;
        dial_phone_number = number1;
        dial_phone_number_code = countryCode1;
    }

    public DialPhoneDialog(Context context, String countryCode1, String countryCode2, String number1, String number2, String watchName, DialPhoneNumberCallBack callBack) {
        super(context);
        this.context = context;
        this.callBack = callBack;
        this.number1 = number1;
        this.number2 = number2;
        this.countryCode1 = countryCode1;
        this.countryCode2 = countryCode2;
        dial_phone_number = number1;
        dial_phone_number_code = countryCode1;
        this.watchName = watchName;
    }

    public DialPhoneDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_dial_phone);

        setDialogAttributes();
        initView();
    }

    private void initView() {
        String preString = getContext().getString(R.string.pre_select_phone_number);

        number_1_iv = (ImageView) findViewById(R.id.number_1_iv);
        number_2_iv = (ImageView) findViewById(R.id.number_2_iv);
        number_1_tv = (TextView) findViewById(R.id.number_1_tv);
        number_2_tv = (TextView) findViewById(R.id.number_2_tv);
        dial_btn = (Button) findViewById(R.id.dial_btn);
        monitor_btn = (Button) findViewById(R.id.monitor_btn);
        number_1_iv.setImageResource(R.drawable.mode_select);

        number_1_rl = findViewById(R.id.number_1_rl);
        number_2_rl = findViewById(R.id.number_2_rl);

        View one_number_ll = findViewById(R.id.one_number_ll);
        View two_number_ll = findViewById(R.id.two_number_ll);
        TextView one_dial_tv = (TextView) findViewById(R.id.one_dial_tv);
        TextView one_monitor_tv = (TextView) findViewById(R.id.one_monitor_tv);
        TextView titleText = (TextView) findViewById(R.id.titleText);

        if (!TextUtils.isEmpty(watchName)) {
            MyFrontUtil.initTextViewText(context, titleText, watchName, "选择", "的号码", 2, watchName.length() + 2);
            MyFrontUtil.initTextViewText(context, one_dial_tv, watchName, "拨打", "的电话", 2, watchName.length() + 2);
            MyFrontUtil.initTextViewText(context, one_monitor_tv, watchName, "远程听", "周围的声音", 3, watchName.length() + 3);

        }

        if (number1 != null && number1.length() > 6) {
            number_1_tv.setText(preString + "1:+" + countryCode1 + number1);
        } else if (number1 != null && countryCode1 != null && number1.length() <= 6 && countryCode1.length() > 0) {
            number_1_tv.setText(preString + "1:+" + countryCode1 + number1);
        } else {
            number_1_tv.setText(preString + "1:" + number1);
        }

        if (number2 != null && number2.length() > 6) {
            number_2_tv.setText(preString + "2:+" + countryCode2 + number2);
        } else if (number2 != null && countryCode2 != null && number2.length() <= 6 && countryCode2.length() > 0) {
            number_2_tv.setText(preString + "2:+" + countryCode2 + number2);
        } else {
            number_2_tv.setText(preString + "2:" + number2);
        }
        if ("".equals(number1)) {
            dial_phone_number_code = countryCode2;
            dial_phone_number = number2;
            isSingleNumber = true;
            one_number_ll.setVisibility(View.VISIBLE);
            two_number_ll.setVisibility(View.GONE);
            number_1_tv.setVisibility(View.GONE);
            number_2_iv.setVisibility(View.GONE);
            number_1_rl.setVisibility(View.GONE);
        }
        if ("".equals(number2)) {
            isSingleNumber = true;
            dial_phone_number_code = countryCode1;
            dial_phone_number = number1;
            one_number_ll.setVisibility(View.VISIBLE);
            two_number_ll.setVisibility(View.GONE);
            number_2_tv.setVisibility(View.GONE);
            number_1_iv.setVisibility(View.GONE);
            number_2_rl.setVisibility(View.GONE);
        }

        if (!isSingleNumber) {
            one_number_ll.setVisibility(View.GONE);
            two_number_ll.setVisibility(View.VISIBLE);
        }


        dial_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.okNumber1(dial_phone_number_code, dial_phone_number, true);
            }
        });
        monitor_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.okNumber1(dial_phone_number_code, dial_phone_number, false);
            }
        });


        number_1_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number_1_iv.setImageResource(R.drawable.mode_select);
                number_2_iv.setImageResource(R.drawable.mode_no_select);
                dial_phone_number = number1;
                dial_phone_number_code = countryCode1;
            }
        });

        number_2_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number_1_iv.setImageResource(R.drawable.mode_no_select);
                number_2_iv.setImageResource(R.drawable.mode_select);
                dial_phone_number = number2;
                dial_phone_number_code = countryCode2;
            }
        });

        findViewById(R.id.one_dial_rl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.okNumber1(dial_phone_number_code, dial_phone_number, true);
            }
        });

        findViewById(R.id.one_monitor_rl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.okNumber1(dial_phone_number_code, dial_phone_number, false);
            }
        });
        findViewById(R.id.dial_btn_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        setCancelable(true);
    }
}
